<!--
 * @Description: 
 * @Author: chenjiaxin
 * @Date: 2022-03-10 20:53:31
 * @LastEditTime: 2022-03-11 07:02:39
 * @FilePath: \WebSkillExtend\eg09-setTimeout时间误差\index.html
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>setTimeout时间误差</title>
  </head>
  <body>
    <script>
      let count = 0; //统计 执行fun 函数次数
      //统计开始时间
      let start = new Date().getTime();
      let interval = 1000; // 1000 ms
      let timeId = setTimeout(fun, interval);
      function fun() {
        count++;
        let now = new Date().getTime();
        let space = now - (start + interval);
        console.log(`第${count}次，时差=`, space);
        if (count < 10) {
          start = new Date().getTime();
          timeId = setTimeout(fun, interval);
        }
      }
    </script>
  </body>
</html>
